package m.framework.web.view;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import m.Config;
import m.framework.utils.ClassUtil;
import m.framework.utils.StrBuilder;
import m.framework.utils.StringUtil;


public class Html {
	private static Logger log = LoggerFactory.getLogger(Html.class);
	

	private static Html html=new Html();
	public static Html getInstance() {
		return html;
	} 
	
	public String getWebRoot(){
		return Config.webRoot();
	}
	
	/**
	 * @param text 文本
	 * @param type tagName
	 * */
	public String field(String text,String controls){
		StrBuilder sb=new StrBuilder();
		sb.append("<div class='control-group'>");
		sb.append("<label class='control-label'>");
		sb.append(text+"：");
		sb.append("</label>");
		
		String auto="";
		if(controls.startsWith("<textarea")){
			auto="control-row-auto";
		}
		sb.append("<div class='controls "+auto+"' >");
		sb.append(controls);
		sb.append("</div>");
		sb.append("</div>");
		return sb.toString();
	}

	public String show(Object obj) {
		try {
			String str = ClassUtil.toString(obj);
			if (StringUtil.isEmpty(str)) {
				return "";
			}
			return str;
		} catch (Exception ex) {
			log.debug("显示对象失败"+obj,ex);
			return "<font color='red'>无法显示" + obj + "<font/>";
		}
	}
	
	/**显示备注,将特殊字符过滤*/
	public String showRemark(String value){
		if(value==null){
			return"";
		}
		value=value.replaceAll(" ", "&nbsp;");
		value=value.replaceAll("<", "&lt;");
		return value.replace("\n", "<br/>");
	}
}
